//
//  YAYIBookMessageCell.m
//  yayi
//
//  Created by hxp on 16/7/12.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIBookMessageCell.h"

#import "YAYIMessageListModel.h"
#import "NSDate+VR.h"


@implementation YAYIBookMessageCell

-(void)setModel:(YAYIMessageListModel *)model
{
    _model = model;
    
    self.content.text = [NSString stringWithFormat:@"%@",_model.title];
//    NSDate *date = [NSDate date];
//    NSDateFormatter *formate = [[NSDateFormatter alloc] init];
//    formate.dateFormat = @"YYYY-MM-dd";
//    NSString *curDate = [formate stringFromDate:date];
//    
//    if ([[YAYIMessageListModel timeString:model.createtime] isEqualToString:curDate]) {
//        self.timeLabel.text = [YAYIMessageListModel timeString:model.createtime];
//    }else{
//        self.timeLabel.text = [YAYIMessageListModel timeString:model.createtime];
//    }
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.createtime / 1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"YYYY-MM-dd";
    
    if ([date isToday]) {
        formatter.dateFormat = @"HH:mm";
    }
    
    self.timeLabel.text = [formatter stringFromDate:date];
    CGSize size = [self.timeLabel.text sizeWithFont:self.timeLabel.font];
    [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(size.width + 12);
    }];
}


- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    self.bgView.backgroundColor = highlighted ? YAYIColor(0xee, 0xee, 0xee) : [UIColor whiteColor];
}


+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"bookMessageCell";
    
    YAYIBookMessageCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        cell = [[YAYIBookMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
        [self setupSubView];
    }
    return self;
}


-(void)setupSubView{
    self.timeLabel = [UILabel new];
    
    self.timeLabel.layer.masksToBounds = YES;
    self.timeLabel.layer.cornerRadius = 5;
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    self.timeLabel.font = [UIFont systemFontOfSize:15];
    self.timeLabel.backgroundColor = [UIColor lightGrayColor];
    self.timeLabel.textColor = [UIColor whiteColor];
    [self.contentView addSubview:self.timeLabel];
    
    self.bgView = [UIView new];
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.cornerRadius = 5;
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bgView];
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = @"预约通知";
    titleLbl.font = [UIFont systemFontOfSize:16];
    [self.bgView addSubview:titleLbl];
    self.titleLbl = titleLbl;
    
    self.content = [UILabel new];
    self.content.font = [UIFont systemFontOfSize:14];
    self.content.numberOfLines = 0;
    
    self.content.textColor = [UIColor colorWithRed:87/255.0 green:85/255.0 blue:85/255.0 alpha:1];
    [self.bgView addSubview:self.content];
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@(15));
        make.centerX.mas_equalTo(self.contentView);
//        make.size.mas_equalTo(CGSizeMake(50, 18));
        make.width.mas_greaterThanOrEqualTo(50);
        make.height.mas_equalTo(18);
    }];
    
    [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLbl.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self.bgView.mas_left).with.offset(12);
        make.right.mas_equalTo(self.bgView.mas_right).with.offset(-12);
    }];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeLabel.mas_bottom).with.offset(10);
        make.left.mas_equalTo(22);
        make.right.mas_equalTo(self.contentView.mas_right).with.offset(-22);
//        make.height.mas_equalTo(height2 + 80);
        make.bottom.mas_equalTo(self.content.mas_bottom).offset(20);
    }];
}


-(CGFloat)heightForRow:(NSString *)str font:(UIFont *)font width:(CGFloat)width
{
//    CGSize size = [str sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    CGSize size = [str sizewithFont:font maxSize:CGSizeMake(width, MAXFLOAT)];
    return size.height;
}


@end
